<template>
  <div>
    <div>
      <BreadX level1="商品管理" level2="商品列表">
         
       </BreadX>
    </div>
    <el-card class="card">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            clearable
            v-model="goodsInfo.query"
            @clear="getsearch"
            @input="getsearch"
            ><el-button
              slot="append"
              icon="el-icon-search"
              @click="getsearch"
            ></el-button
          ></el-input>
        </el-col>
        <el-col :span="16">
          <el-button type="primary" @click="addlist">添加商品</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-table border :data="gooddata" style="margin-top: 15px">
          <el-table-column label="#" type="index"></el-table-column>
          <el-table-column
            label="商品名称"
            prop="goods_name"
            width="700px"
          ></el-table-column>
          <el-table-column
            label="商品价格"
            prop="goods_price"
          ></el-table-column>
          <el-table-column
            label="商品重量"
            prop="goods_weight"
          ></el-table-column>
          <el-table-column label="创建时间">
            <template slot-scope="scope">
              {{ scope.row.add_time | dateTime }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
              ></el-button>
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="deletgoods(scope.row.goods_id)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="goodsInfo.pagenum"
          :page-sizes="[2, 4, 5, 8]"
          :page-size="goodsInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import BreadX from "@/components/BreadX.vue";
import _ from "lodash";
export default {
  components: { BreadX },
  data() {
    return {
      goodsInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      gooddata: [],
      total: 0,
      input: "",
    };
  },
  created() {
    this.goodslist();
  },
  methods: {
    //   添加商品
    addlist() {
      this.$router.push("/Addlist");
    },
    // 删除商品
    async deletgoods(goods_id) {
      const res = await this.$confirm("确定要删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除",
        });
      });
      if (res === "confirm") {
        await this.$API.DeletegoodsAPI(goods_id);
        this.goodslist();
      }
    },
    // 搜索
    getsearch: _.debounce(function () {
      this.goodslist();
    }, 300),
    // 商品列表
    async goodslist() {
      const res = await this.$API.GoodsAPIlist(this.goodsInfo);
      console.log("商品管理列表：", res);
      //   let {total,goods} = res
      this.gooddata = res.goods;
      this.total = res.total;
    },
    // 每页几条
    handleSizeChange(v) {
      console.log(`每页条`, v);
      this.goodsInfo.pagesize = v;
      this.goodslist();
    },
    // 当前页
    handleCurrentChange(v) {
      console.log(`当前页`, v);
      this.goodsInfo.pagenum = v;
      this.goodslist();
    },
  },
};
</script>

<style lang="scss" scoped>
.card {
  margin-top: 30px;
}
</style>